class Pin(robject):
    Servo = None

    @ld
    def __init__(*a,**k):pass

    @ld
    def as_pwm(*a,**k):pass

    @ld
    def as_servo(*a,**k):pass


    #def to_string(self):
    def __str__(self):
        return '%s(%s)[%s]'%(self.name,self.gpio,self.mode)

    def idle(self):
        if self.servo :
            self.servo.mcu_idle()

    def stop(self):
        if self.servo :
            self.servo.mcu_stop()
            self.servo = self.servo.free()

        if self.pwm:
            self.pwm.duty(0)
            self.pwm = None

        self.instance(0)

    def __call__(self,*argv):
        if self.pwm:
            #FIXME return pwm value
            #if not len(argv):
            duty=int(argv[0])
            if self.pwm_max and (abs(duty) > self.pwm_max):
                return self.pwm.duty(self.pwm_max)
            if duty<0: duty=0
            return self.pwm.duty(duty)

        if self.servo:
            return self.set_pos(*argv)

        if not len(argv):
            return self.value()

        if argv[0]:
            self.hi()
        else:
            self.lo()

#raw access no state inversion
    def dw(self,v):
        self.instance(( v and 1 ) or 0 )
        return self

    def dr(self):
        return self.instance.value()

    def di(self):
        self.dw( not self.instance.value() )
        return self.instance.value()

#Hi level logic output/read
    def hi(self):
        self.instance.value(self.histate)
        return self

    def lo(self):
        self.instance.value(not self.histate)
        return self

    def value(self):
        if self.histate:
            return self.instance.value()
        return not self.instance.value()

    def toggle(self):
        self.dw( not self.instance.value() )
        return self.value()
